/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.guice;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.google.code.sapien.util.PropertiesUtils;

/**
 * Default Implementation of the {@link com.google.code.sapien.guice.Configuration} interface.
 * @author Adam
 * @version $Id: DefaultConfig.java 16 2009-04-12 05:05:02Z a.ruggles $
 * 
 * Created on Feb 5, 2009 at 9:32:24 PM 
 */
public class DefaultConfig implements Configuration {
	/**
	 * A Map to store the settings.
	 */
	private final Map<String, Object> settings;

	/**
	 * Constructs a Loader Object.
	 */
	public DefaultConfig() {
		settings = Collections.synchronizedMap(new HashMap<String, Object>());
		final Properties properties = PropertiesUtils.load(DEFAULT_SETTINGS);
		for (Map.Entry<Object, Object> entry : properties.entrySet()) {
			settings.put(String.valueOf(entry.getKey()), entry.getValue());
		}
	}

	/**
	 * Constructs a Loader Object.
	 * @param file The settings file.
	 */
	public DefaultConfig(final String file) {
		settings = Collections.synchronizedMap(new HashMap<String, Object>());
		final Properties properties = PropertiesUtils.load(file);
		for (Map.Entry<Object, Object> entry : properties.entrySet()) {
			settings.put(String.valueOf(entry.getKey()), entry.getValue());
		}
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.guice.Configuration#getAsProperties()
	 */
	public synchronized Properties getAsProperties() {
		final Properties props = new Properties();
		for (Map.Entry<String, Object> entry : settings.entrySet()) {
			props.put(entry.getKey(), entry.getValue());
		}
		return props;
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.guice.Configuration#getSetting(java.lang.Class,
	 * com.google.code.sapien.guice.ConfigSetting)
	 */
	public <T> T getSetting(final Class<T> valueClass, final ConfigSettings loaderSetting) {
		return valueClass.cast(settings.get(loaderSetting.toString()));
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.guice.Configuration#getSetting(java.lang.Class, java.lang.String)
	 */
	public <T> T getSetting(final Class<T> valueClass, final String name) {
		return valueClass.cast(settings.get(name));
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.guice.Configuration#getSetting(com.google.code.sapien.guice.ConfigSettings)
	 */
	public Object getSetting(final ConfigSettings loaderSetting) {
		return settings.get(loaderSetting.toString());
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.guice.Configuration#getSetting(java.lang.String)
	 */
	public Object getSetting(final String name) {
		return settings.get(name);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.guice.Configuration#setSetting(com.google.code.sapien.guice.ConfigSettings,
	 * java.lang.Class, java.lang.Object)
	 */
	public <T> void setSetting(final ConfigSettings loaderSetting, final Class<T> valueClass, final T value) {
		settings.put(loaderSetting.toString(), value);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.guice.Configuration#setSetting(com.google.code.sapien.guice.ConfigSettings,
	 * java.lang.Object)
	 */
	public void setSetting(final ConfigSettings loaderSetting, final Object value) {
		settings.put(loaderSetting.toString(), value);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.guice.Configuration#setSetting(java.lang.String, java.lang.Class, java.lang.Object)
	 */
	public <T> void setSetting(final String name, final Class<T> valueClass, final T value) {
		settings.put(name, value);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.guice.Configuration#setSetting(java.lang.String, java.lang.Object)
	 */
	public void setSetting(final String name, final Object value) {
		settings.put(name, value);
	}

}
